/*
 This file 'Slider' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2014 Lincong

 Contact:  
 Email: lincong1987@gmail.com

 QQ: 159257119

 See Usage at http://www.jplatformx.com/firebird

 Create date: 2017-02-08 15:48
 */

define(function (require, exports, module) {

	require("Firebird.ui.Component");
	require("Firebird.lang.Uuid");
	require("../pips/jquery-ui-slider-pips.css");
	require("../pips/jquery-ui-slider-pips");

	/**
	 * @class Firebird.ui.Slider
	 *
	 * @example
	 */
	Firebird.define("Firebird.ui.Slider", {
		extend: "Firebird.ui.Component",
		statics: {
			_uuid: 1,
			uuid: function () {
				return this._uuid++;
			},
			attrs: function ($component) {
				var attrs = {};
				attrs = $.extend({}, {
					renderTo: $component.get(0),
					instanceId: $component.attr("fb-instance") || $component.attr("name") || $component.attr("id") || ("slider_" + Firebird.ui.Slider.uuid()),

					hidden: Firebird.ui.Component.eval($component.attr("fb-hidden")),


					prefix: $component.attr("fb-prefix"),
					suffix: $component.attr("fb-suffix"),

					//animate
					//classes
					disabled: Firebird.ui.Component.eval($component.attr("fb-disabled") || $component.prop("disabled")),
					max: Firebird.ui.Component.eval($component.attr("fb-max") || "100"),
					min: Firebird.ui.Component.eval($component.attr("fb-min") || "0"),
					orientation: $component.attr("fb-orientation") || "horizontal",
					range: Firebird.ui.Component.eval($component.attr("fb-range") || false),
					step: Firebird.ui.Component.eval($component.attr("fb-step")),
					value: Firebird.ui.Component.eval($component.attr("fb-value")) || $component.val() || 0,

					/**
					 * {0} {1}
					 */
					label: $component.attr("fb-label") || ""


				});
				return attrs;
			}
		},
		config: {
			renderTo: null,
			instanceId: null,
			width: "100%",
			disabled: false,
			max: 100,
			min: 0,
			/**
			 * Determines whether the slider handles move horizontally (min on left, max on right) or vertically (min on bottom, max on top). Possible values: "horizontal", "vertical".
			 */
			orientation: "horizontal",
			range: false,
			step: 1,
			value: null,
			label: "",

			prefix: "",
			suffix: "",

			/**
			 * 模式
			 * normal 普通
			 * double 双数时，value 为的一个数组，其长度为2
			 */
			mode: "normal"
		},


		/**
		 * wrapCls
		 */
		wrapCls: "fb-ui-slider",

		instance: null,

		_init: function () {
			var me = this;
			var uuid = me.uuid = Firebird.lang.Uuid.generate();
			var renderTo = me.renderTo = $(me.getRenderTo());
			me.instance = renderTo.data({
				instance: me,
				xtype: "slider"
			}).css("display", "none");

			me.render();

			me.wrapper = $("#" + uuid + "_wrapper");
			me.slider = $("#" + uuid + "_slider");
			me.handler = $("#" + uuid + "_handler");

			me.wrapper.append(me.instance);

			me.options = $.extend(true, {}, {
				orientation: me.getOrientation(),
				range: false,
				step: me.getStep(),
				max: me.getMax(),
				min: me.getMin(),
				change: function (e, ui) {
					var value = ui.value;
					me._setValue(value);
					me.proxy("onChange").apply(me, [e, me, ui]);
				},
				start: function (e, ui) {
					var value = ui.value;
					me._setValue(value);
					me.proxy("onStart").apply(me, [e, me, ui]);
				},
				stop: function (e, ui) {
					var value = ui.value;
					me._setValue(value);
					me.proxy("onStop").apply(me, [e, me, ui]);
				},
				slide: function (e, ui) {
					var value = ui.value;
					me._setValue(value);
					me.proxy("onSlide").apply(me, [e, me, ui]);
				},
				create: function (e, ui) {
					var value = ui.value;
					me.proxy("onCreate").apply(me, [e, me, ui]);
				}
			});

			var value = me.getValue();

			if (value && value.toString().indexOf(",") != -1) {
				alert("Firebird.ui.Slider 不允许双选");
			}

			me.slider.slider(me.options).slider("pips", {
				rest: "label",
				prefix: me.getPrefix(),
				suffix: me.getSuffix()
			});

			me.setDisabled(me.getDisabled());

			// me.setWidth(me.getWidth());
			me.setValue(me.getValue());
			//me.setLabel(me.getLabel());
		},

		render: function () {

			var me = this;
			me.instance.after("" +
				"<div id='" + me.uuid + "_wrapper' class='" + me.wrapCls + "-wrapper'>" +
				"	<div  id='" + me.uuid + "_slider'  class='" + me.wrapCls + "-slider'>" +
				// "		<div  id='" + me.uuid + "_handler'  class='ui-slider-handle " + me.wrapCls + "-handler'></div>" +
				"	</div>" +
				"</div>");

		},


		proxy: function (func) {
			if (!this[func]) {
				return function () {
				};
			} else {
				this.instance.trigger(func);
				return this[func];
			}
		},

		applyOrientation: function (orientation) {
			this.option("orientation", orientation);
			return orientation;
		},
		applyDisabled: function (status) {
			this.option("disabled", status);
			return status;
		},

		applyClasses: function (icons) {
			this.option("icons", icons);
			return icons;
		},

		applyLabel: function (label) {
			this.handler && this.handler.html(label);
			return label;
		},

		applyRange: function (range) {
			this.option("range", range);
			return range;
		},

		applyHidden: function (status) {
			if (this.instance) {
				var status = !!status;
				this.instance.css({display: status ? "none" : ""});
			}
			return status;
		},

		applyValue: function (val) {
			if (this.instance && val != null && typeof (val) != "undefined") {
				this.option("value", val);
			}
			return val;
		},

		_setValue: function (val) {
			if (this.instance) {
				this.value = val;
				this.instance.val(val);
			}
		},

		updateValue: function (val) {
			if (this.instance && val != null && typeof (val) != "undefined") {
				this.option("value", val);
			}
			return val;
		},

		widthTimer: 0,
		applyWidth: function (width) {
			var me = this;
			if (this.wrapper) {
				setTimeout(function () {
					if (width.toString().indexOf("%") > 0) {

						clearTimeout(me.widthTimer);
						me.widthTimer = setTimeout(function () {
							me.wrapper.width(width);
						}, 16);

						me.wrapper.parent().off("resize").on("resize", function () {
							clearTimeout(me.widthTimer);
							me.widthTimer = setTimeout(function () {
								me.wrapper.width(width);
							}, 16);
						});

					} else {
						try {
							width = parseInt(width, 10);
							me.wrapper.parent().off("resize");

						} catch (e) {
						}
						me.wrapper.width(width);
					}

				}, 16);
			}
			return width;
		},


		destroy: function () {
			this.slider.slider("destroy");
		},
		option: function (key, value) {
			if (typeof key == "undefined" && typeof value == "undefined") {
				return this.slider && this.slider.slider("option");
			}
			if (typeof value == "undefined") {
				return this.slider && this.slider.slider("option", key);
			}
			if (typeof key != "undefined" && typeof value != "undefined") {
				this.slider && this.slider.slider("option", key, value);
			}
			return this;
		}
	});

	module.exports = Firebird.ui.Slider;

});